/**
 * Created by JasonChou on 16/5/7.
 */

import {data as cityList } from '../libs/city'
import Vue from 'vue';

export var parseCity = function (code, type) {
    if (parseInt(code) > 100000 && parseInt(code) < 999999) {

        var proNum, cityNum, areaNum;
        var accC, accA;

        proNum = Math.floor(code / 10000) * 10000;
        cityNum = Math.floor(code / 100) * 100;
        areaNum = +code;

        //确定在哪一级返回
        accC = (proNum == cityNum);
        accA = (cityNum == areaNum);

        //直辖市作为特例
        if (proNum == 110000 || proNum == 120000 || proNum == 310000 || proNum == 500000) {
            cityNum = proNum;
            accC = true;
        }

        function back() {
            switch (type) {
                case 'object':
                {
                    return {
                        province: cityList[proNum][0] || null,
                        city: accC ? null : cityList[proNum][cityNum][0],
                        area: accA ? null : cityList[proNum][cityNum][areaNum]
                    }
                }
                case 'code':
                {
                    return {
                        province: proNum || null,
                        city: accC ? null : cityNum,
                        area: accA ? null : areaNum
                    }
                }
                default :
                    return cityList[proNum][0] + ' ' + (accC ? '' : cityList[proNum][cityNum][0]) + ' ' + (accA ? '' : cityList[proNum][cityNum][areaNum]);
            }
        }

        if (accC)return back();
        if (accA)return back();
        return back();
    }

    return null;
};

Vue.filter('parseCity', parseCity);
